//index.js
//获取应用实例
const app = getApp()
var api = require('../../utils/api.js');  //引入utils/apj.js
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),

  //  -----------------------------------------------------------
    movies: [],
    cur_index: 0,
    index: 0,
    item: ["简约","中式","欧式","美式"],
    obj1: {
      a: 1,
      b: 2
    },
    obj2: {
      b: 3,
      c: 4
    },
    a: 5,
    stylecases: [[], [], [], []],
    img1url: 'http://gcc.gnu.org/img/gccegg-65.png',

    imgarray: [{
      code: '',
      data: {
        list:[{
          id: ''
        },{
          post_title: ""
        },{
          more:[{contact_count:''},{sign_count: ''},{thumbnail: ""}]
        }]
      },
      msg: Boolean
    }],

    tabnav: 0,

    currentIndex: 0,
    "firstList": [{ name: 'w券1', money: '5.00' }, { name: 'w券2', money: '50.00'}],
    "secondList": [{ name: 'y券1', money: '10.00' }, { name: 'y券2', money: '20.00' }],
    "thirdList": [{ name: 'g券1', money: '30.00' }, { name: 'g券2', money: '40.00' }],

    loupanlist: [{},{},{},{}],
    gongyilist: [],

  //  -----------------------------------------------------------
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    };

  //  --------------------------------------------------------
    this.loadswiper();
    this.loadcases();
    this.loadgongyi();
    console.log(this.ietm);
    this.loadloupan();
    console.log("Hello, World!!!");
    api.getparams();
  //  --------------------------------------------------------
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
//  -----------------------------------------------------------
  loadcases(){
    api.get({
      url: '/portal/lists/getCategoryPostLists',
      data: {
        category_id: 2,
        field: 'post.id,post.post_title,post.more',
        is_top: 1,
        limit:12,
        order: '+list_order'

      },
      success: data=>{
        if(data.code=='1'){
          let stylecases=this.data.stylecases;
          let arr=data.data.list;
          for(var i=0; i<arr.length;i++){
            stylecases[arr[i].more.style].push(arr[i])  //第一遍向stylecases[2]中压入arr[0],相当如动态的将arr数组的各个子项压入stylecases数组中
          }
          console.log(stylecases)
          this.setData({
            stylecases: stylecases
          })
        }
      }
    })
  },

  loadswiper(){
    api.get({
      url: 'home/slides/1',
      success: data=>{
        console.log(data)
        if(data.code==1){
          this.setData({
            movies: data.data[0].items
          })
        }
      }
    });
  },

  changetab: function(e){

    this.setData({
      cur_index: e.currentTarget.dataset.index
    })
    console.log(this.data.cur_index)
  },

  tabclicked: function (e) {
    this.setData({
      tabnav: e.currentTarget.dataset.tab
    })

  },
  //swiper切换时会调用
  pagechange: function (e) {
    if("touch"===e.detail.source){
      let currentPageIndex=this.data.currentIndex
      currentPageIndex=(currentPageIndex+1)%3
      this.setData({
        currentIndex: currentPageIndex
      })
    }
  },
  //用户点击tab时调用
  titleClick: function (e) {
    let currentPageIndex=
        this.setData({
          //拿到当前索引并动态改变
          currentIndex: e.currentTarget.dataset.idx
        })
  },

  //加载楼盘
  loadloupan(){
    api.get({
      url:'/portal/lists/getCategoryPostLists',
      data: {
        category_id: 1,
        filed: "post.id,post.post_title,post.more",
        is_top: 1,
        order: '-post.id'
      },
      success:data=>{
        if(data.code=='1'){
          this.setData({
            loupanlist: data.data.list
          })
        }
      }
    });
    console.log(loupanlist)
  },
//  -----------------------------------------------------------

  // 加载3d案例
  load3dcase(){
    api.get({
      url: "portal/lists/getCategoryPostLists",
      data: {
        category_ic: 4,
        filed: "post.id, post.post_title,post.more",
        is_top: 1,
        limit: 1,
        order: "-post.id"
      },
      success: data=>{
        console.log(data.data)
        if(data.code=='1'){
          this.setData({
            case3d: data.data.list[0]
          })
        }
      }
    })
  },

  loadgongyi(){
    api.get({
      url: "/portal/lists/getCategoryPostLists",
      data:{
        category_id: 8,
        filed: "post.id,post.post_title,post.more",
        is_top: 1,
        order: "-post.id"
      },
      success: data=>{
        if(data.code=='1'){
          this.setData({
            gongyilist: data.data.list
          })
        }
      }
    })
  }
})

